/**
 * This software is released under the terms of the MIT License
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @copyright  2009-2011 Roberto Perpuly
 * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
 */
#ifndef __RunConsolePanelClass__
#define __RunConsolePanelClass__

/**
@file
Subclass of RunConsolePanelGeneratedClass, which is generated by wxFormBuilder.
*/

#include <plugins/wxformbuilder/RunConsolePluginGeneratedClass.h>
#include <PluginClass.h>
#include <widgets/ProcessWithHeartbeatClass.h>
#include <vector>

/** Implementing RunConsolePanelGeneratedClass */

namespace mvceditor {
	
class RunConsolePanelClass : public RunConsolePanelGeneratedClass {
protected:
	
	/**
	 * Handle the clear button.
	 */
	void OnClear(wxCommandEvent& event);

public:
	/** 
	 * Constructor 
	 * For all of these pointer arguments, this class will NOT own them.
	 */
	RunConsolePanelClass(wxWindow* parent, EnvironmentClass* environment, StatusBarWithGaugeClass* gauge, int id = wxID_ANY);

	/**
	 * Set to run the given PHP file
	 * 
	 * @param const wxString& fullPath full path to the PHP file to run
	 */
	void SetToRunFile(const wxString& fullPath);
	
	/**
	 * Handle the 'Run' button
	 */
	void RunCommand(wxCommandEvent& event);
	
	/**
	 * set the focus on the text input for the command.
	 */
	void SetFocusOnCommandText();

	/**
	 * when the page is closed perform cleanup. This means
	 * clean up the child process and any open gauges
	 */
	void OnPageClose(wxAuiNotebookEvent& evt);

private:
		
	/**
	 * The command to run
	 */
	wxString CommandString;
	
	/**
	 * Used to run the process asynchronously
	 */
	ProcessWithHeartbeatClass ProcessWithHeartbeat;

	/**
	 * project holds the File System location of PHP executable
	 * 
	 * @var EnvironmentClass*
	 */
	EnvironmentClass* Environment;
	
	/**
	 * To show progess to the user
	 * 
	 * @var StatusBarWithGaugeClass
	 */
	StatusBarWithGaugeClass* Gauge;
	
	/**
	 * the current PID of the async process.
	 */
	long int CurrentPid;
	
	/**
	 * Gauge id for this instance of RunConsole. Each run console will have its own gauge to update
	 * 
	 * @var int
	 */
	int IdProcessGauge;
	
	/**
	 * called when the process ends.
	 */
	void OnProcessComplete(wxCommandEvent& event);

	/**
	 * called when the process ends.
	 */
	void OnProcessFailed(wxCommandEvent& event);
	
	/**
	 * Check running process output. Will poll process output and display it.
	 * 
	 * @param wwCommandEvent& event
	 */
	void OnProcessInProgress(wxCommandEvent& event);
	
	DECLARE_EVENT_TABLE()
};

class RunConsolePluginClass : public PluginClass {

public:

	/**
	 * Constructor
 	 */
	RunConsolePluginClass();
	
	/**
	 * Add menu items
	 */
	void AddToolsMenuItems(wxMenu* toolsMenu);
	
	/**
	 * Add items to the toolbar
	 */
	void AddToolBarItems(wxAuiToolBar* toolBar);

	void AddKeyboardShortcuts(std::vector<DynamicCmdClass>& shortcuts);

	private:
				
		/**
		 * handler for the menu
		 */
		void OnRunFileAsCli(wxCommandEvent& event);
		
		/**
		 * Always run in a new console window
		 */
		void OnRunFileAsCliInNewWindow(wxCommandEvent& event);
		
		/**
		 * disable menus when source code notebook is empty
		 */
		void OnUpdateUi(wxUpdateUIEvent& event);
		
		/**
		 * Our menu items
		 */
		wxMenuItem* RunCliMenuItem;
		wxMenuItem* RunCliWithArgsMenuItem;
		wxMenuItem* RunCliInNewWindowMenuItem;
		wxMenuItem* RunCliWithArgsInNewWindowMenuItem;
		
		DECLARE_EVENT_TABLE()
};

}
#endif // __RunConsolePanelClass__
